﻿using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using SuperSocket;

namespace Lawn.AppServer
{
    public class SubMessageHandler:IMessageHandler
    {
        
        private ILogger<SubMessageHandler>  logger;
        private ISessionManager sessionManager;
        public SubMessageHandler(ILogger<SubMessageHandler> _logger,ISessionManager _sessionManager)
        {
            this.logger = _logger;
            this.sessionManager = _sessionManager;
        }
        public async Task Handler(IAppSession Session, MessagePackageInfo messagePackageInfo)
        {

            // string topic = Encoding.UTF8.GetString(messagePackageInfo.Body.ToArray());
            if(messagePackageInfo==null) return;
            var topic = messagePackageInfo.Topic;
            logger.LogInformation($"topic:{topic}");
            if (!string.IsNullOrEmpty(topic))
            {
                await sessionManager.Add(Session.SessionID, topic);
            } 
         
            await Task.CompletedTask;

        }
    }
}